package com.stationbuilding.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.stationbuilding.R;
import com.stationbuilding.adapter.DialogShowImageAdapter;
import com.stationbuilding.bean.SelectFileBean;
import com.stationbuilding.fragment.ImageFragment;
import java.util.ArrayList;
import java.util.List;

public class ZoomImageDialog extends DialogFragment implements ImageFragment.OnItemClickListener {

    private ViewPager viewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    private List<SelectFileBean> fileList;
    private int position;

    public ZoomImageDialog(List<SelectFileBean> bitmapList, int position) {
        this.fileList = bitmapList;
        this.position = position;
    }

    //全屏显示
    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            dialog.getWindow().setLayout(width, height);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //full screen dialog fragment
        setStyle(DialogFragment.STYLE_NORMAL, R.style.FragmentDialogTheme);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_zoom_image, container);
        initViewPager(view);
        return view;
    }

    public void initViewPager(View view) {
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        initFragments();
        DialogShowImageAdapter adapter = new DialogShowImageAdapter(getChildFragmentManager(), fragmentList);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(position);
    }

    private void initFragments() {
        ImageFragment imageFragment;
        for (SelectFileBean selectFileBean : fileList) {
            Bundle bundle = new Bundle();
            bundle.putInt("type", selectFileBean.getType());
            bundle.putString("path", selectFileBean.getPath());
            imageFragment = new ImageFragment();
            imageFragment.setArguments(bundle);
            imageFragment.setOnItemClickListener(this);
            fragmentList.add(imageFragment);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onItemClickListener() {
        dismiss();
    }
}
